package com.qianfeng.wuyang.himalaya.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.Entity_AdTop;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AdActivity extends AppCompatActivity implements Callback<Entity_AdTop>, View.OnClickListener {

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private ImageView imageView;
    private boolean ad = false;

    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
        imageView = (ImageView) findViewById(R.id.ad_image);

        imageView.setOnClickListener(this);

        preferences = getSharedPreferences("himalaya", Context.MODE_PRIVATE);
        editor = preferences.edit();

        sleepThread();
    }

    @Override
    protected void onStart() {
        super.onStart();
        MyApplication application = (MyApplication) getApplication();
        application.getAdService().getAdList("0", "android", "loading", "wifi", "0", "4.3.98").enqueue(this);
    }

    @Override
    public void onResponse(Call<Entity_AdTop> call, Response<Entity_AdTop> response) {
        if (preferences.getBoolean("boo", false) | response.body().getList().size() == 1) {
            Picasso.with(this).load(response.body().getList().get(0).getCover()).into(imageView);
            url = response.body().getList().get(0).getLink();
        } else {
            Picasso.with(this).load(response.body().getList().get(1).getCover()).into(imageView);
            url = response.body().getList().get(1).getLink();
        }
        editor.putBoolean("boo", !preferences.getBoolean("boo", false));
        editor.commit();
    }

    @Override
    public void onFailure(Call<Entity_AdTop> call, Throwable t) {
    }

    private void sleepThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    synchronized (this) {
                        if (ad) {
                            this.wait();
                        }
                    Intent intent = new Intent(AdActivity.this, MainActivity.class);
                    startActivity(intent);
                    }
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onClick(View view) {
        ad = true;
        Intent intent = new Intent(this, Classify_ad_Activity.class);
        if (url != null) {
            intent.putExtra("url", url);
        }
        startActivity(intent);
    }
}
